<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class tracking extends Controller
{
	private  $_template_f; 
	
	function __construct()
	{
		parent::Controller();
		$permitid = $this->session->userdata('permitid');
		if(!$this->session->userdata('uname') || !is_numeric($permitid) || $permitid > -1)
		{
			redirect(site_url('login/'));
			die();
		}
		$this->_lang = $this->mycommon->getLangCode();	
		$this->_template_f = $this->config->item('template_f');
		$this->load->language('tracking/tracking', $this->_lang);
		$this->load->helper('breadcrumb');
        $this->load->model('tracking/tracking_model');
	}
	
	function trackingBannerDown()
	{
		$page = (int)$this->input->get('page');
		if($page < 1) $page = 1;
		$data = array();
		//Get date param
		$date = new DateTime();
		$data['todate'] = $date->format('Y-m-d');
		$date = $date->sub(new DateInterval('P6D'));
		$data['fromdate'] = $date->format('Y-m-d');
		
		$data['page'] = $page;
		$data['limit'] = 20;
		$data['items'] = $this->tracking_model->selfserving_report_banner_tracking($page, $data['limit']);
		$data['pages'] = $data['items']['pages'];
		$data['items'] = $data['items']['items'];
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f.'tracking/tracking_banner_down', $data);
		$this->_loadFooter();
	}
	
	function trackingBannerUp()
	{
		$page = (int)$this->input->get('page');
		if($page < 1) $page = 1;
		$data = array();
		//Get date param
		$date = new DateTime();
		$data['todate'] = $date->format('Y-m-d');
		$date = $date->sub(new DateInterval('P6D'));
		$data['fromdate'] = $date->format('Y-m-d');
		
		$data['page'] = $page;
		$data['limit'] = 20;
		$data['items'] = $this->tracking_model->selfserving_system_tracking_banner_running_showly($page, $data['limit']);
		$data['pages'] = $data['items']['pages'];
		$data['items'] = $data['items']['items'];
		
		$this->_loadHeader();
		$this->_loadMenu();
		$this->load->view($this->_template_f.'tracking/tracking_banner_up', $data);
		$this->_loadFooter();
	}
    
    function trackingBannerRunning()
    {
        $this->_loadHeader();
		$this->_loadMenu();
        
        $data['page'] = $page = $this->input->get('page') ? $this->input->get('page') : '1';
        $data['rownum'] = $rownum = $this->input->get('rownum') ? $this->input->get('rownum') : '20';
        
        $data['typegroup'] = $typegroup = $this->input->get('typegroup') ? $this->input->get('typegroup'): -1;
        $data['groupid'] = $groupid = $this->input->get('groupid') ? $this->input->get('groupid'): -1;
        $data['key'] = $key = $this->input->get('key') ? $this->input->get('key'):'';
        $data['lstgroupid'] = $this->tracking_model->selfserving_get_group_by_type($typegroup);
        $data['typeview'] = $typeview = $this->input->get('typeview') ? $this->input->get('typeview'): 2;
        
        $temp = $this->tracking_model->selfserving_tracking_js_get_lstbanners($typegroup,$groupid,$key,$page,$rownum,$typeview);
        $data['info']= $temp['items'];
        $data['totalpage']= $temp['totalpage'];
        $data['tracking_lang'] = $this->lang->line('tracking_lang');
		$this->load->view($this->_template_f.'tracking/tracking_banner_running',$data);
		$this->_loadFooter();
    }
    
    function getGroupByType()
    {
        $typegroup = $this->input->get('value');
        if(!in_array($typegroup,array(-1,1,2)))
        {
            $typegroup=-1;
        }
        $lstgroupid = $this->tracking_model->selfserving_get_group_by_type($typegroup);
        echo '<select class="payment-by-month" id="filterGroup">';
        foreach($lstgroupid as $gr){
            echo '<option value="'.$gr['groupid'].'">'.$gr['groupname'].'</option>';
        }
        echo '</select>';
    }
    
    function zone()
    {
        $this->_loadHeader();
		$this->_loadMenu();
        $data['bannerid'] = $bannerid = $this->input->get('bannerid');

        $data['info'] = $this->tracking_model->selfserving_tracking_js_get_lstzone_by_bannerid($bannerid);
        
        $data['tracking_lang'] = $this->lang->line('tracking_lang');
		$this->load->view($this->_template_f.'tracking/tracking_zone',$data);
		$this->_loadFooter();
    }
	
	function _loadHeader($title = '')
	{
		$header = array();
		$header['title'] = $title == '' ? 'AdMarket' : $title;
		$header['template_f'] = $this->_template_f;
		
		$header['username'] = '';
		$header['userid'] = '';
		$header['permitid'] = '';
		$header['module_name'] = 'budget';
		$header['lang'] = $this->_lang;
		$header['common_lang'] = $this->lang->line('common_lang');
        
		if($this->session->userdata('uname'))
		{
			$header['username'] = $this->session->userdata('uname');
			$header['userid'] = $this->session->userdata('uid');
			$header['permitid'] = $this->session->userdata('permitid');
		}
		
		 //load header
		$this->load->view($this->_template_f . 'preheader_view');
		$this->load->view($this->_template_f . 'header_view', $header);
	}
	
	function _loadMenu()
	{
		$data = array();
		$data['username'] = '';
		$data['userid'] = '';
		$data['permitid'] = '';
		$data['lang'] = $this->_lang;
		if($this->session->userdata('uname'))
		{
			$data['username'] = $this->session->userdata('uname');
			$data['userid'] = $this->session->userdata('uid');
			$data['permitid'] = $this->session->userdata('permitid');
		}
		$data['common_lang'] = $this->lang->line('common_lang');
        
		$this->load->view($this->_template_f . 'menu_view',$data);
	}
	
	function _loadFooter()
	{
		$this->load->view($this->_template_f . 'footer_view');
	}
}